<?php
namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Exception;

class Shichangorder extends Api{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**发布求购**/
    public function sendorder(){
        $this->error('市场功能正在升级中...');
        $userinfo = $this->auth->getUserinfo();
        $one_price = $this->request->param('one_price');
        $num = $this->request->param('num');
        $pay_type = $this->request->param('pay_type');
        if($one_price=='' && $num=='' && $pay_type==''){
            $this->error('参数不全');
        }
        $shichangorderModel = new \app\admin\model\shichangorder\Shichangorder();
        $indata['buy_user_id']=$userinfo['id'];
        $indata['nums']=$num;
        $indata['one_price']=$one_price;
        $indata['all_price']=$num*$one_price;
        $indata['pay_type']=$pay_type;
        $indata['create_time']=time();
        $shichangorderModel->insert($indata);
        $this->success('发布成功');
    }

    /**自由市场列表**/
    public function orderlist(){
        $num = $this->request->param('num');
        $page = $this->request->param('page');
        $pageSize =10;
        $start = ($page-1)*$pageSize;
        $shichangorderModel = new \app\admin\model\shichangorder\Shichangorder();
        if($num==0){
            $list = collection($shichangorderModel
                ->alias('s')
                ->join('user u', 'u.id = s.buy_user_id')
                ->field('s.*, u.nickname')
                ->where(['s.status'=>0])
                ->order('s.id desc')
                ->limit($start,$pageSize)
                ->select());
        }else{
            $where['s.nums']=['<=',$num ];
            $where['s.status']=['=',0 ];
            $list = collection($shichangorderModel
                ->alias('s')
                ->join('user u', 'u.id = s.buy_user_id')
                ->field('s.*, u.nickname')
                ->where($where)
                ->order('s.id desc')
                ->limit($start,$pageSize)
                ->select());
        }
        if($list){
            $list = $list->toArray();
            $newlist = [];
            foreach ($list as $k=>$v){
                $newlist[$k]['id'] = $v['id'];
                $newlist[$k]['one_price'] = $v['one_price'];
                $newlist[$k]['nums'] = $v['nums'];
                $newlist[$k]['nickname'] = $v['nickname'];
                $newlist[$k]['pay_type'] = $v['pay_type'];
            }
            $this->success('成功',$newlist);
        }else{
            $this->success('成功');
        }
    }

    /**出售**/
    public function sell(){
        $id = $this->request->param('id');
        $userinfo = $this->auth->getUserinfo();
        $shichangorderModel = new \app\admin\model\shichangorder\Shichangorder();
        $info = $shichangorderModel->where(['id' => $id])->find()->toArray();
        if($info['status']!=0){
            $this->error('不是发布中的状态，不能出售');
        }
        if($info['nums']>$userinfo['shouyi_balance']){
            $this->error('您帐户的HC余额不足');
        }
        $payarr=explode(',',$info['pay_type']);
        if(in_array(1,$payarr)){
            if($userinfo['is_wx'] == 0){
                return json_encode([
                    'code'=>101,
                    'msg'=>'您还未绑定微信收款码',
                    'data'=>[]
                ]);
            }
        }
        if(in_array(2,$payarr)){
            if($userinfo['is_zfb'] == 0){
                return json_encode([
                    'code'=>101,
                    'msg'=>'您还未绑定支付宝收款码',
                    'data'=>[]
                ]);
            }
        }
        if(in_array(3,$payarr)){
            if($userinfo['is_sh'] == 0){
                return json_encode([
                    'code'=>101,
                    'msg'=>'您还未绑定纷享生活收款码',
                    'data'=>[]
                ]);
            }
        }
        $shichangorderModel->where(['id' => $id])->update(['status' => 1,'jiedan_time'=>time(),'sell_user_id'=>$userinfo['id']]);
        $this->success('出售成功，请等待买家付款');
    }

    /**发布方付款**/
    public function pay(){
        $id = $this->request->param('id');
        $img = $this->request->param('img');
        $userinfo = $this->auth->getUserinfo();
        $shichangorderModel = new \app\admin\model\shichangorder\Shichangorder();
        $info = $shichangorderModel->where(['id' => $id])->find()->toArray();
        if($info['status']!=1){
            $this->error('不是待付款状态不能支付');
        }
        $shichangorderModel->where(['id' => $id])->update([
            'status' => 2,
            'pay_type'=>$img,
            'pay_time'=>time(),
            ]);
        $this->success('支付成功，请等待卖家确认');
    }

    /**卖家确认**/
    public function sure(){
        $id = $this->request->param('id');
        $shichangorderModel = new \app\admin\model\shichangorder\Shichangorder();
        $userModel = new \app\admin\model\User();
        $userinfo = $this->auth->getUserinfo();
        $info = $shichangorderModel->where(['id' => $id])->find()->toArray();
        if($info['status']!=2){
            $this->error('不是已支付状态，不能确认');
        }
        if($info['nums']>$userinfo['shouyi_balance']){
            $this->error('您帐户的HC余额不足');
        }
        Db::startTrans();
        try{
            $shichangorderModel->where(['id' => $id])->update([
                'sure_time'=>time(),
                'status'=>3
            ]);
            //增加买方HC
            $userModel->where(['id'=>$info['buy_user_id']])->setInc('shouyi_balance',$info['nums']);
            //减少卖方的HC
            $userModel->where(['id'=>$info['sell_user_id']])->setDec('shouyi_balance',$info['nums']);
            Db::commit();
            $this->success('确认成功');
        }catch(Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    /**我是买家**/
    public function buylist(){
        $page = $this->request->param('page');
        $pageSize = 10;
        $start =($page-1)*$pageSize;
        $type = $this->request->param('type');
        $userinfo = $this->auth->getUserinfo();
        $shichangorderModel = new \app\admin\model\shichangorder\Shichangorder();
        if($type==0){//全部
            $list = collection($shichangorderModel
                ->alias('s')
                ->join('user u', 'u.id = s.buy_user_id')
                ->field('s.*, u.nickname')
                ->where(['s.buy_user_id'=>$userinfo['id']])
                ->order('s.id desc')
                ->limit($start,$pageSize)
                ->select());
        }elseif ($type==1){//发布中
            $list = collection($shichangorderModel
                ->alias('s')
                ->join('user u', 'u.id = s.buy_user_id')
                ->field('s.*, u.nickname')
                ->where(['s.buy_user_id'=>$userinfo['id'],'s.status'=>0])
                ->order('s.id desc')
                ->limit($start,$pageSize)
                ->select());
        }elseif ($type==2){//待付款
            $list = collection($shichangorderModel
                ->alias('s')
                ->join('user u', 'u.id = s.sell_user_id')
                ->field('s.*, u.nickname')
                ->where(['s.buy_user_id'=>$userinfo['id'],'s.status'=>1])
                ->order('s.id desc')
                ->limit($start,$pageSize)
                ->select());
        }elseif ($type==3){//待确认
            $list = collection($shichangorderModel
                ->alias('s')
                ->join('user u', 'u.id = s.sell_user_id')
                ->field('s.*, u.nickname')
                ->where(['s.buy_user_id'=>$userinfo['id'],'s.status'=>2])
                ->order('s.id desc')
                ->limit($start,$pageSize)
                ->select());
        }elseif ($type==4){//已完成
            $list = collection($shichangorderModel
                ->alias('s')
                ->join('user u', 'u.id = s.sell_user_id')
                ->field('s.*, u.nickname')
                ->where(['s.buy_user_id'=>$userinfo['id'],'s.status'=>3])
                ->order('s.id desc')
                ->limit($start,$pageSize)
                ->select());
        }
        if($list){
            $list = $list->toArray();
            $newlist = [];
            foreach ($list as $k=>$v){
                $newlist[$k]['id'] = $v['id'];
                $newlist[$k]['one_price'] = $v['one_price'];
                $newlist[$k]['nickname'] = $v['nickname'];
                $newlist[$k]['pay_type'] = $v['pay_type'];
                $newlist[$k]['nums'] = $v['nums'];
                $newlist[$k]['status'] = $v['status'];
            }
            $this->success('成功',$newlist);
        }else{
            $this->success('成功',[]);
        }
    }

    /**卖家列表**/
    public function selllist(){
        $page = $this->request->param('page');
        $pageSize = 10;
        $start =($page-1)*$pageSize;
        $type = $this->request->param('type');
        $userinfo = $this->auth->getUserinfo();
        $shichangorderModel = new \app\admin\model\shichangorder\Shichangorder();
        if($type==0){//全部
            $list = collection($shichangorderModel
                ->alias('s')
                ->join('user u', 'u.id = s.buy_user_id')
                ->field('s.*, u.nickname')
                ->where(['s.sell_user_id'=>$userinfo['id']])
                ->order('s.id desc')
                ->limit($start,$pageSize)
                ->select());
        }elseif ($type==1){//待收款
            $list = collection($shichangorderModel
                ->alias('s')
                ->join('user u', 'u.id = s.buy_user_id')
                ->field('s.*, u.nickname')
                ->where(['s.sell_user_id'=>$userinfo['id'],'s.status'=>1])
                ->order('s.id desc')
                ->limit($start,$pageSize)
                ->select());
        }elseif ($type==2){//待确认
            $list = collection($shichangorderModel
                ->alias('s')
                ->join('user u', 'u.id = s.buy_user_id')
                ->field('s.*, u.nickname')
                ->where(['s.sell_user_id'=>$userinfo['id'],'s.status'=>2])
                ->order('s.id desc')
                ->limit($start,$pageSize)
                ->select());
        }elseif ($type==3){//已完成
            $list = collection($shichangorderModel
                ->alias('s')
                ->join('user u', 'u.id = s.buy_user_id')
                ->field('s.*, u.nickname')
                ->where(['s.sell_user_id'=>$userinfo['id'],'s.status'=>3])
                ->order('s.id desc')
                ->limit($start,$pageSize)
                ->select());
        }
        if($list){
            $list = $list->toArray();
            $newlist = [];
            foreach ($list as $k=>$v){
                $newlist[$k]['id'] = $v['id'];
                $newlist[$k]['one_price'] = $v['one_price'];
                $newlist[$k]['nums'] = $v['nums'];
                $newlist[$k]['status'] = $v['status'];
                $newlist[$k]['nickname'] = $v['nickname'];
                $newlist[$k]['pay_type'] = $v['pay_type'];
            }
            $this->success('成功',$newlist);
        }else{
            $this->success('成功',[]);
        }
    }

    /**订单详情**/
    public function orderdetail(){
        $id = $this->request->param('id');
        $shichangorderModel = new \app\admin\model\shichangorder\Shichangorder();
        $info = $shichangorderModel->where(['id'=>$id])->find()->toArray();
        if($info['status']==0){
            $this->success('成功',$info);
        }else{
            $info = $shichangorderModel
                ->alias('s')
                ->join('useraccount u', 'u.user_id = s.sell_user_id')
                ->join('user n', 'n.id = s.buy_user_id')
                ->field('s.*, n.nickname,u.wx_img,u.zfb_img,u.sh_img')
                ->where([])
                ->find()->toArray();
            $this->success('成功',$info);
        }

    }
}